package com.apes.fico.finance.model.newcost;

import com.apes.fico.finance.model.costcollect.TypeCost;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = NewOperatingCosts.CONST_TABLE_NAME)
public class NewOperatingCosts extends Model {
    public final static String CONST_TABLE_NAME = "new_operating_costs";

    @Id
    @Column(name = "id", nullable = false, length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "经营成本", padding = 8 ,prefix = "JY")
    @Comment("编号")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司")
    private PartyRole company;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
    @Comment("部门")
    private Dept dept;

    @Column(name = "source_receipt")
    @Comment("源单据")
    private String sourceReceipt;

    @Column(name = "source_id")
    @Comment("源单号")
    private String sourceId;

    public void setSourceId(String sourceId) {
        if (StringUtils.isNotEmpty(sourceReceipt)){
            if ("人员工资".equals(sourceReceipt)){
                this.sourceId = "GZ"+sourceId;
            }else if ("水电租".equals(sourceReceipt)){
                this.sourceId = "SD"+sourceId;
            }
        }else {
            this.sourceId = sourceId;
        }
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @Name
    @JoinColumn(name = "person_id", referencedColumnName = "id")
    @Comment("人员")
    private PartyRole person;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "declare_company", referencedColumnName = "id")
    @Comment("申报公司")
    private PartyRole declareCompany;

    @NotNull
    @Column(name = "year_month")
    @Comment("年月")
    private Date yearMonth;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cost_type", referencedColumnName = "id")
    @Comment("成本类型")
    private Enumeration costType;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "type_cost", referencedColumnName = "id")
    @Comment("成本类别")
    private TypeCost typeCost;

    @Column(name = "accrual_date")
    @Comment("计提时间")
    private Date accrualDate;

    @Column(name = "accrual_amt", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("计提金额")
    private double accrualAmt = 0;

    @Column(name = "real_date")
    @Comment("实际发生时间")
    private Date realDate;

    @Column(name = "real_amt", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("实际发生金额")
    private double realAmt = 0;

    @Column(name = "blunt_provision_date")
    @Comment("冲计提时间")
    private Date bluntProvisionDate;

    @Column(name = "blunt_provision_amt", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("冲计提金额")
    private double bluntProvisionAmt = 0;

    @Column(name = "blunt_actually_date")
    @Comment("冲实际发生时间")
    private Date bluntActuallyDate;

    @Column(name = "blunt_actually_amt", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("冲实际发生金额")
    private double bluntActuallyAmt = 0;

    @Column(name = "accrual_state")
    @Comment("计提状态") //done(完成)
    private String accrualState;

    @Column(name = "real_state")
    @Comment("实际发生状态")//done(完成)
    private String realState;

    @OneToMany(mappedBy = "newOperatingCosts", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<NewOperatingCostsItem> newOperatingCostsItems = new HashSet<>();

    public Set<NewOperatingCostsItem> getOperatingCostsItemSet() {
        if (newOperatingCostsItems == null) newOperatingCostsItems = new HashSet<>();
        for (NewOperatingCostsItem item : newOperatingCostsItems) {
            item.setNewOperatingCosts(this);
        }
        return newOperatingCostsItems;
    }
}
